package io.renren.system.oss.cloud;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.SDKGlobalConfiguration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectResult;
import io.renren.common.exception.ErrorCode;
import io.renren.common.exception.RenException;
import io.renren.system.oss.tool.MiMETools;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 紫光云存储
 */
@Slf4j
public class ZiGuangCloudStorageService extends AbstractCloudStorageService {
    public ZiGuangCloudStorageService(CloudStorageConfig config) {
        this.config = config;
    }

    public static AmazonS3 getAmazonS3Client(String accessKey, String secretKey, String endPoint) {
        System.setProperty(SDKGlobalConfiguration.DISABLE_CERT_CHECKING_SYSTEM_PROPERTY, "true");
        BasicAWSCredentials cred = new BasicAWSCredentials(accessKey, secretKey);
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setProtocol(Protocol.HTTP);
        AmazonS3 s3 = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(cred))
                .withClientConfiguration(clientConfiguration)
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, endPoint))
                .build();
        return s3;
    }


    @Override
    public String upload(byte[] data, String path) {
        return upload(new ByteArrayInputStream(data), path);
    }

    @Override
    public String upload(InputStream inputStream, String path) {
        try {
            AmazonS3 amazonS3Client = getAmazonS3Client(config.getZgAccessKeyId(), config.getZgAccessKeySecret(), config.getZgEndPoint());

            ObjectMetadata objectMetadata = new ObjectMetadata();
            // 创建上传的Object的Metadata
            objectMetadata.setContentLength(inputStream.available());
            objectMetadata.setCacheControl("no-cache");
            objectMetadata.setHeader("Pragma", "no-cache");
            if (StrUtil.isNotEmpty(MiMETools.parseContentType(path))) {
                objectMetadata.setContentType(MiMETools.parseContentType(path));
            }

            PutObjectResult result = amazonS3Client.putObject(config.getZgBucketName(), path, inputStream, objectMetadata);
            amazonS3Client.setObjectAcl(config.getZgBucketName(), path, CannedAccessControlList.PublicRead);
            if (result != null) {
                log.info("文件上传成功{}", path);
            }

            // 设置权限(公开读)
            /*client.putObject(config.getAliyunBucketName(), path, inputStream);
            client.shutdown();*/
        } catch (Exception e) {
            log.error("异常信息{}", ExceptionUtil.getMessage(e));
            throw new RenException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e, "");
        }
        // FILE_URL = "https://" + newBucket + "." + endPoint + "/" + fileUrl;
        // return  "https://" + config.getZgBucketName() + "." + config.getZgEndPoint() + "/" + path;
        return config.getZgStaticDomain() + "/" + path;

    }

    @Override
    public String uploadSuffix(byte[] data, String suffix) {
        return upload(data, getPath(config.getZgPrefix(), suffix));
    }

    @Override
    public String uploadSuffix(InputStream inputStream, String suffix) {
        return upload(inputStream, getPath(config.getZgPrefix(), suffix));
    }
}
